// packageH/sign_shopV2/component/company/company.js
const app = getApp();

Component({
  /**
   * 组件的属性列表
   */
  properties: {
    show: {
      type: Boolean,
      value: false
    },
    person_name: {
      type: String,
      value: ''
    },
    is_new: {
      type: Boolean,
      value: false
    },
    cid:{
      type: [Boolean,String],
      value: '0'
    },
  },
  observers: {
    'show': function(valA,valB) {
      this.setData({
        showCompany: valA
      })
    },
  },
  pageLifetimes: {
    show: function() {
      // 页面被展示
      this.getAgreement();
    }
  },
  /**
   * 组件的初始数据
   */
  data: {
    showCompany: false,
    agreement: {},
    form: {
      name: "",
      org_code: "",
      legal_name: "",
      legal_idNo: "",
      org_type: "1"
    }
  },

  /**
   * 组件的方法列表
   */
  methods: {
    // 表单修改
    handlerInput(event) {
      let type = event.target.dataset.type;
      this.setData({
        [`${type}`]: event.detail
      });
    },
    checkoutInfo() {
      if (!this.data.form.name) {
        app.tips("请填写组织名称");
        return;
      }
      let queryUrl = app.getNetAddresss("plugin.yun-sign.frontend.company.qccCheck");
      app._postNetWork({
        url: queryUrl,
        data: {
          name: this.data.form.name
        },
        success: (resdata) => {
          let res = resdata.data;
          if (res.result === 1) {
            this.setData({
              'form.org_code': res.data.org_code
            });
          } else {
            wx.showModal({
              title: "提示",
              content: res.msg,
              showCancel: false
            });
          }
        },
        fail(err) {
          console.log(err, "----失败-----");
        }
      });
    },
    getAgreement() {
      let queryUrl = app.getNetAddresss("plugin.yun-sign.frontend.company.get-agreement");
      app._postNetWork({
        url: queryUrl,
        success: (resdata) => {
          let res = resdata.data;
          if (res.result === 1) {
            this.setData({
              agreement: res.data
            });
          } else {
            wx.showModal({
              title: "提示",
              content: res.msg,
              showCancel: false
            });
          }
        },
        fail(err) {
          console.log(err, "----失败-----");
        }
      });
    },
    hiddenPop(){
      this.setData({
        showCompany: false
      })
    },
    sureIdentify () {
      if(!this.data.form.legal_name) {
        app.tips("请填写姓名");
        return;
      }
      if(!this.data.form.legal_idNo) {
        app.tips("请选择身份证");
        return;
      }
  
      if(!this.data.form.org_type) {
        app.tips("请选择证件号类型");
        return;
      }
  
      if(!this.data.form.name) {
        app.tips("请填写组织名称");
        return;
      }
  
      if(!this.data.form.org_code) {
        app.tips("请填写证件号");
        return;
      }
  
      this.authRequest();
    },
    authRequest () {
      let that = this;
      let queryUrl = app.getNetAddresss("plugin.yun-sign.frontend.h5.company.auth-company-v2");
      let json = {...that.data.form};
      if(!this.properties.is_new) json.cid = this.properties.cid;
      app._postNetWork({
        url: queryUrl,
        data: json,
        success (resdata) {
          let res = resdata.data;
          if (res.result === 1) {
            app.tips(res.msg);
            this.triggerEvent('changeCid',{cid: res.data.cid});
            this.hiddenPop();
          } else {
            wx.showModal({
              title: "提示",
              content: res.msg,
              showCancel: false,
              success () {}
            });
          }
        },
        fail (err) {
          console.log(err, "----失败-----");
        }
      });
    },
  }
})